package com.zyiot.lbs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.view.ActionBarView;
import com.zyiot.bdapi.LBSCloudSearch;
import com.zyiot.bdapi.StartNavi;
import com.zyiot.cache.ImageDownloader;
import com.zyiot.cache.OnImageDownload;
import com.zyiot.model.Eshop;

public class EshopDetailActivity extends Activity {
	Context context;
	InitApplication app;
	public static final int MSG_NET_TIMEOUT = 100;
	public static final int MSG_NET_STATUS_ERROR = 200;
	public static final int MSG_NET_SUCC = 1;
	UMSocialService controller;
	Eshop eshop;
	ImageDownloader mDownloader;
	String DESCRIPTOR;
	String column_id;
	String column_key;
	String column_value;
	String shareURL = "";
	private MKSearch mSearch = null;
	ActionBarView socializeActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_eshop_detail);
		context = this;
		app = (InitApplication) getApplication();
		eshop = (Eshop) getIntent().getSerializableExtra("detail");

		// 初始化搜索模块，注册搜索事件监听
		mSearch = new MKSearch();
		mSearch.init(app.mBMapManager, mksearchlistener);
		// 百度短串
		int latitude = (int) (eshop.getLatitude() * 1E6);
		int longitude = (int) (eshop.getLongitude() * 1E6);
		final GeoPoint sharePoint = new GeoPoint(latitude, longitude);
		mSearch.poiRGCShareURLSearch(sharePoint, eshop.getName(), eshop.getAddr());

		LayoutInflater layoutInflater = getLayoutInflater();
		// 详情页
		View merchant = layoutInflater.inflate(R.layout.layout_eshop_merchant, null);
		FrameLayout frammermerchant = (FrameLayout) findViewById(R.id.merchant);
		frammermerchant.addView(merchant);

		TextView eshop_detail_header_title = (TextView) findViewById(R.id.eshop_detail_header_title);
		eshop_detail_header_title.setText(eshop.getName());

		TextView merchant_address_text = (TextView) findViewById(R.id.merchant_address_text);
		merchant_address_text.setText(eshop.getAddr());
		merchant_address_text.setOnClickListener(btnnavi_listener);
		ImageView merchant_address = (ImageView) findViewById(R.id.merchant_address);
		merchant_address.setOnClickListener(btnnavi_listener);

		TextView eshop_detail_header_price = (TextView) findViewById(R.id.eshop_detail_header_price);
		eshop_detail_header_price.setText(eshop.getPrice());

		TextView merchant_phone_text = (TextView) findViewById(R.id.merchant_phone_text);
		merchant_phone_text.setText(eshop.getPhone());
		merchant_phone_text.setOnClickListener(btnphone_listener);
		ImageView merchant_phone = (ImageView) findViewById(R.id.merchant_phone);
		merchant_phone.setOnClickListener(btnphone_listener);

		TextView merchant_info_text = (TextView) findViewById(R.id.merchant_info_text);
		merchant_info_text.setText(eshop.getInfo());

		DESCRIPTOR = eshop.getGeotable_id() + ":" + eshop.getUid() + ":" + eshop.getName();
		// 测试主题
		// DESCRIPTOR = app.DESCRIPTOR;
		controller = UMServiceFactory.getUMSocialService(DESCRIPTOR, RequestType.SOCIAL);
		final FrameLayout frammercomments = (FrameLayout) findViewById(R.id.comments);
		View progressbar = layoutInflater.inflate(R.layout.progressbar, null);
		frammercomments.addView(progressbar);

		Handler sHandler = new Handler();
		sHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				//addWXPlatform();
				if (!controller.getEntity().mInitialized) {
					controller.setShareContent("【" + eshop.getName() + "】" + eshop.getAddr() + " 快来体验下吧！" + shareURL);
				}
				// 操作栏
				socializeActionBar = new ActionBarView(context, DESCRIPTOR);
				frammercomments.removeAllViews();
				frammercomments.addView(socializeActionBar);
				controller.getConfig().setDefaultShareLocation(false);
				// 设置平台
				
				controller.getConfig().setPlatforms(SHARE_MEDIA.TENCENT);
				// String appID = "wxee75bf73fa63bf51";
				// // 微信图文分享必须设置一个url
				// String contentUrl = "http://www.umeng.com/social";

				column_id = eshop.getUid();
				column_key = "rating";
				column_value = String.valueOf(controller.getEntity().getLikeCount());
			}
		}, 5000);

		// int flag =
		// SocializeConstants.FLAG_USER_CENTER_LOGIN_VERIFY|SocializeConstants.FLAG_USER_CENTER_HIDE_LOGININFO;
		// controller.openUserCenter(context, flag);

		// Log.d(app.TAG, app.DESCRIPTOR + ":" +
		// controller.getEntity().customID);

		Button btnback = (Button) findViewById(R.id.btnback);
		btnback.setOnClickListener(btnback_listener);
		TextView helpphone = (TextView) findViewById(R.id.helpphone);
		helpphone.setOnClickListener(helpphone_listener);

		final ImageView imageview = (ImageView) findViewById(R.id.eshop_detail_header_image);
		if (mDownloader == null) {
			mDownloader = new ImageDownloader();
		}
		String imagePath = "/zyiot";
		String url = eshop.getImageurl();
		if (mDownloader != null && !url.equals("")) {
			mDownloader.imageDownload(url, imageview, imagePath, EshopDetailActivity.this, new OnImageDownload() {
				@Override
				public void onDownloadSucc(Bitmap bitmap, String c_url) {
					imageview.setImageBitmap(bitmap);
				}
			});
		}
	}

	/**
	 * @功能描述 : 添加微信平台分享
	 * @return
	 */

	private void addWXPlatform() {

		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = "wxee75bf73fa63bf51";
		// 微信图文分享必须设置一个url
		String contentUrl = "http://http://s.zyiot.com/";
		// 添加微信平台
		controller.getConfig().supportWXPlatform(context, appId, contentUrl);
		// 支持微信朋友圈
		controller.getConfig().supportWXCirclePlatform(context, appId, contentUrl);

	}

	// 云搜索handle
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_NET_TIMEOUT:
				break;
			case MSG_NET_STATUS_ERROR:
				break;
			case MSG_NET_SUCC:
				String result = msg.obj.toString();
				Log.i("TEST", "status:" + result);
				break;
			}
		};

	};

	/*
	 * 云存储更新喜欢值
	 */
	private void update() {

		LBSCloudSearch.UPDATE_COLUMN_V2(column_id, column_key, column_value, mHandler, InitApplication.networkType);
	}

	OnClickListener btnback_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			column_value = String.valueOf(controller.getEntity().getLikeCount());
			update();
			finish();
		}
	};

	MKSearchListener mksearchlistener = new MKSearchListener() {
		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult res, int error) {
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult result, int iError) {
		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
		}

		@Override
		public void onGetPoiDetailSearchResult(int type, int error) {
		}

		@Override
		public void onGetAddrResult(MKAddrInfo res, int error) {
		}

		@Override
		public void onGetShareUrlResult(MKShareUrlResult result, int type, int error) {
			// 分享短串结果
			shareURL = result.url;
		}

		@Override
		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			Log.d("TEST", "onGetPoiResult");
		}
	};

	OnClickListener helpphone_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			callphone(getString(R.string.help_phone_call));
		}
	};
	OnClickListener btnphone_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String phonenumber = eshop.getPhone();
			callphone(phonenumber);
		}
	};
	OnClickListener btnnavi_listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int endlat = (int) (eshop.getLatitude() * 1E6);
			int endlong = (int) (eshop.getLongitude() * 1E6);

			GeoPoint endPoint = new GeoPoint(endlat, endlong);
			if (app.currlocation != null) {
				int startlat = (int) (app.currlocation.getLatitude() * 1E6);
				int startlong = (int) (app.currlocation.getLongitude() * 1E6);
				GeoPoint startPoint = new GeoPoint(startlat, startlong);
				StartNavi.startNavi(context, EshopDetailActivity.this, startPoint, endPoint);
			}
		}
	};

	private void callphone(String phonenumber) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenumber));
		startActivity(intent);
	}

	@Override
	protected void onStop() {
		Looper.loop();
		column_value = String.valueOf(controller.getEntity().getLikeCount());
		update();
		super.onStop();
	}
}
